<template>
	<view class="addAddres">
		添加新地址
		<view class="content">
			<view class="row">
				<view class="nominal">收件人</view>
				<view class="input">
					<input v-model="name" placeholder="请输入收件人姓名" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">电话号码</view>
				<view class="input">
					<input v-model="phoneNumber" placeholder="请输入收件人电话号码" />
				</view>
			</view>
			<view class="row" >
				<view class="nominal">所在地址</view>
				<view class="input" @tap="simp">
					请点击选择地址
					
				</view>
			</view>
			<view class="row" >
				<view class="nominal">地址</view>
				<view class="input">
					<input v-model="detailAddress" placeholder="请输入详细地址" />
				</view>
			</view>
			<view class="row" >
				<view class="nominal">设置默认地址</view>
				<view class="input">
					<switch :checked="swh"  @change="switch1Change" />
				</view>
			</view>
		</view>
		<button @click="bu"> 保存地址</button>
		<button  v-if="editType" @tap="del">删除地址</button>
		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" ></simple-address>
	</view>
</template>

<script>
	import simpleAddress from '@/components/simple-address/simple-address.vue';
	import {
		 addAddressInfo
	} from "../../../../api/user/addAddres/index.js"
	import {
		findAllAddress,
		delAddressInfo
	} from "@/api/user/Addres/index.js"
	export default {
		components: {
		       simpleAddress
		   },
		data() {
			return {
				cityPickerValueDefault: [0, 0, 1],
				addressid:"", //存上个页面传递过来的id
				editType:false,
				pickerText:"",
				swh:"true",
				name:"",
				phoneNumber:"",
				detailAddress:"",
				defaultStatus:1,
				city:"",
				province:"",
				region:"",
				cityCode:"",
			};
		},
		
		methods:{
			// 是否默认选中
			 switch1Change(e){
				
				 if(e.detail.value){
					this.defaultStatus=1;
				 }else{
					 this.defaultStatus=0;
				 }
				 console.log(e);
				 
			 },
			 //地址栏
			 onConfirm(e){
				 // this.pickerText = JSON.stringify(e);
				console.log("选择弹窗里地址",e);
				// console.log(this.pickerText);
				this.cityCode = e.provinceCode+','+e.cityCode+','+e.areaCode;
				
				console.log(this.cityCode);
				this.city = e.labelArr[1];
				this.province = e.labelArr[0];
				this.region = e.labelArr[2];
				
			 },
			 //控制地址弹窗
			simp(){
				   this.$refs.simpleAddress.open();
			},
			//提交地址
			bu(){
				addAddressInfo({
					 "city": this.city, //城市 
					  "cityCode": this.cityCode, //地址编码拼接
					  "createTime": "", 
					  "defaultStatus":this.defaultStatus, //设置默认; 0 -> 否 ; 1-> 是 ,
					  "detailAddress":this.detailAddress, //详细地址(街道) ,
					  "id": "",
					  "memberId": "",
					  "modifyTime": "",
					  "name":this.name, //收货人名称 ,
					  "phoneNumber": this.phoneNumber, //收货人电话
					  "postCode": "", 
					  "province": this.province,//省份/直辖市 ,
					  "region": this.region, //区 ,
					  "tag": ""
				}).then(res=>{
					console.log(res);
					if(res.success){
						uni.showToast({
							title:"添加成功",
						}),
						// 定时器
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
						},1000)
					}
				})
			},
			
			//根据id获取地址
			getAddressById(id){
				findAllAddress()
				.then(res =>{
					// 查询所有列表匹配id
					var info = res.data.items.find(ele =>ele.id == id);
					console.log(info);
					// // 获取弹窗里面的地址下标
					// var index = this.$refs.simpleAddress.queryIndex(info.cityCode.split("-"), 'value');
					// this.cityPickerValueDefault = index.index;
					// 	console.log(this.cityPickerValueDefault );
						this.addressInfo = info;
						this.name = info.name;
						this.phoneNumber = info.phoneNumber;
						this.region = {
							cityCode:info.cityCode,
							label:`${info.province}-${info.city}-${info.region}`,
							// value:[info.province,info.city,info.region]
						};
						this.detailAddress = info.detailAddress;
						this.isDefault = info.defaultStatus ? true : false;
				})
			},
			// 点击删除地址
			del(){
				uni.showModal({
				  title: "删除提示",
				  content: "你将删除这个收货地址",
				  success: res => {
				    if (res.confirm) {
				      // 发送请求删除地址
							delAddressInfo(this.addressInfo.id)
							.then(res =>{
								if(res.success == true){
									uni.showToast({
										title:"删除成功"
									});
									setTimeout(()=>{
										uni.navigateBack({
											delta:1
										})
									},1000)
									
								}
							})
				    } else if (res.cancel) {
				    }
				  }
				});
			}
		},
		onLoad(e) {
			console.log("监听页面打印",e);
			// 如果有addressId 说明是编辑
			if(e.addressId){
				this.editType = true;
				this.addressid = e.addressId;
				this.getAddressById(e.addressId);
			}else{
				this.editType = false
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	.addAddres{
		.content{
			// display: flex;
			.row{
				display: flex;
				align-items: center;
				width: 94%;
				margin: 0 3%;
				border-bottom: 2rpx solid #ccc;
				.nominal{
					// margin:  0 auto;
					line-height: 120rpx;
					width: 30%;
					height: 120rpx;
					font-weight: 200;
					font-size: 30rpx;
				}
				.input{
					
				}
			}
		}
	}

</style>
